package com.xidian.zhiancloud.controller.emergencyManagement;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xidian.zhiancloud.constant.EmergencyManagementConstant;
import com.xidian.zhiancloud.constant.LedgerManagementConstant;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.controller.ledgerManagerment.relatedPart.Student;
import com.xidian.zhiancloud.controller.ledgerManagerment.relatedPart.Teacher;
import com.xidian.zhiancloud.dao.RevisionRecordMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.dto.emergencyManagement.EmergencyPlanManagementDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.*;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.emergencyManagement.EmergencyPlanManagementQO;
import com.xidian.zhiancloud.service.emergencyManagement.EmergencyPlanExecService;
import com.xidian.zhiancloud.service.emergencyManagement.EmergencyPlanManagementService;
import com.xidian.zhiancloud.service.emergencyManagement.RevisionRecordService;
import com.xidian.zhiancloud.service.user.UserService;
import com.xidian.zhiancloud.service.user.usermanage.OrganizationService;
import com.xidian.zhiancloud.util.zyf.ExcelUtils;
import com.xidian.zhiancloud.vo.accidentmessage.AccidentMessageVO;
import com.xidian.zhiancloud.vo.emergencyManagement.*;
import com.xidian.zhiancloud.vo.ledgerManagerment.relatedParty.RelatedPartyBlackListVO;
import com.xidian.zhiancloud.vo.ledgerManagerment.relatedParty.RelatedPartyPeopleDocVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 应急管理 应急预案管理接口
 */
@RestController
@RequestMapping("/EmergencyPlanManagement")
public class EmergencyPlanManagementController extends BaseController {

    @Autowired
    private EmergencyPlanManagementService emergencyPlanManagementService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private EmergencyPlanExecService emergencyPlanExecService;

    /**
     * 增加应急预案管理
     *
     * @param emergencyPlanManagementDTO 应急预案对象
     * @return 返回是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(EmergencyPlanManagementDTO emergencyPlanManagementDTO) {
        emergencyPlanManagementDTO.getEmergencyPlanManagement().setCompanyId(getSessionUserCompanyId());
        emergencyPlanManagementDTO.getEmergencyPlanManagement().setCreaterId(getSessionUserid());
        emergencyPlanManagementDTO.getEmergencyPlanManagement().setCreateTime(new Date(System.currentTimeMillis()));
        Boolean res = emergencyPlanManagementService.add(emergencyPlanManagementDTO);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id获取应急预案管理
     *
     * @param id 应急预案管理对象的id
     * @return 应急预案管理对象
     */
    @GetMapping("/getById")
    public ResultDTO<EmergencyPlanManagementDTOVO> getDTOVOById(@RequestParam("id") Long id) {
        return ResultDTO.successOf(emergencyPlanManagementService.getDTOVOById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id删除应急预案管理
     *
     * @param ids ids数组
     * @return 返回是否删除成功
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {
        Boolean res = emergencyPlanManagementService.deleteByIds(ids);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改应急预案管理
     *
     * @param emergencyPlanManagementDTO 应急预案管理对象
     * @return 返回是否修改成功
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(EmergencyPlanManagementDTO emergencyPlanManagementDTO) {
        emergencyPlanManagementDTO.getEmergencyPlanManagement().setCompanyId(null);
        emergencyPlanManagementDTO.getEmergencyPlanManagement().setCreaterId(null);
        emergencyPlanManagementDTO.getEmergencyPlanManagement().setCreateTime(null);
        Boolean res = emergencyPlanManagementService.patchById(emergencyPlanManagementDTO);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 条件查询
     *
     * @param emergencyPlanManagementQO 查询条件
     * @return 返回查询列表
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<EmergencyPlanManagementVO>> getListByQueryObject(EmergencyPlanManagementQO emergencyPlanManagementQO) {
        if (emergencyPlanManagementQO.getCurrent() == null || emergencyPlanManagementQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        emergencyPlanManagementQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(emergencyPlanManagementService.getListByQO(emergencyPlanManagementQO), GeneralResponseEnums.SUCCESS);
    }

//    @GetMapping("/getRevisionRecord")
//    public  ResultDTO<List<RevisionRecordVO>> getRevisionRecord(@RequestParam("id") Long id){
//        return ResultDTO.successOf(emergencyPlanManagementService.getRevisionRecord(id),GeneralResponseEnums.SUCCESS);
//    }
//
//    @GetMapping("/getReviewnRecord")
//    public  ResultDTO<List<ReviewRecordVO>> getReviewRecord(@RequestParam("id") Long id){
//        return ResultDTO.successOf(emergencyPlanManagementService.getReviewRecord(id),GeneralResponseEnums.SUCCESS);
//    }

    /**
     * 导出
     *
     * @param response
     * @param ids      id列表
     * @throws IOException
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, Long[] ids) throws IOException {
        List<EmergencyPlanManagement> list = emergencyPlanManagementService.listByIds(Arrays.asList(ids));
        List<EmergencyPlanManagementVO> ans = new LinkedList<>();
        list.forEach(o -> {
            EmergencyPlanManagementVO vo = new EmergencyPlanManagementVO();
            BeanUtils.copyProperties(o, vo);
            SysUser user = userService.getById(vo.getCreaterId());
            vo.setCreaterName(user == null ? "该用户已被删除" : user.getName());
            Organization org = organizationService.getById(vo.getOrganizationId());
            vo.setOrganizationName(org == null ? "该部门不存在" : org.getName());
            vo.setEmergencyPlanTypeName(EmergencyManagementConstant.EmergencyPlanType.get(vo.getTypeId()));
            ans.add(vo);
        });
        ExcelUtils.exportExcel(ans, "应急预案管理", "应急预案管理", EmergencyPlanManagementVO.class, "应急预案管理", response);
    }

    @PostMapping("/editStatus")
    public ResultDTO<Boolean> editStatus(@RequestParam("id")Long id) {
        EmergencyPlanManagement emergencyPlanManagement = emergencyPlanManagementService.getById(id);
        if (emergencyPlanManagement == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.GET_FAILED);
        }
//        emergencyPlanManagement.setId(id);
//        emergencyPlanManagement.setStatus(status);
        //启动
        if (emergencyPlanManagement.getStatus() == 1) {
//            EmergencyPlanExec emergencyPlanExec = new EmergencyPlanExec();
//            emergencyPlanExec.setEmergencyPlanId(id);
//            emergencyPlanExec.setName(emergencyPlanManagement.getName());
//            emergencyPlanExec.setStartTime(new Date(System.currentTimeMillis()));
//            emergencyPlanExec.setStartUser(getSessionUserid());
            emergencyPlanManagement.setStatus(0);
            emergencyPlanManagementService.updateById(emergencyPlanManagement);
        } else  {
            //结束
//            LambdaQueryWrapper<EmergencyPlanExec> query = Wrappers.<EmergencyPlanExec>lambdaQuery();
//            query.eq(EmergencyPlanExec::getEmergencyPlanId, id).orderByDesc(EmergencyPlanExec::getStartTime);
//            List<EmergencyPlanExec> list = emergencyPlanExecService.list(query);
//            EmergencyPlanExec emergencyPlanExec = list.get(0);
//            if (emergencyPlanExec == null || emergencyPlanExec.getEndUser() != null || emergencyPlanExec.getEndTime() != null) {
//                return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
//            }
//            emergencyPlanExec.setEndTime(new Date(System.currentTimeMillis()));
//            emergencyPlanExec.setEndUser(getSessionUserid());
//            emergencyPlanExecService.updateById(emergencyPlanExec);
            emergencyPlanManagement.setStatus(1);
            emergencyPlanManagementService.updateById(emergencyPlanManagement);
        }
        return ResultDTO.successOf(emergencyPlanManagementService.updateById(emergencyPlanManagement), GeneralResponseEnums.SUCCESS);
    }
}
